package utility.editor;

import game.Level;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.CanvasGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

public class MapEditor extends BasicGame implements ActionListener{
	Level level;
	JFrame frame;
	
	JMenuItem newItem, openItem, saveItem;
	
	AsciiLevel ascii;
	
	public static final String title = "\"Passive\" Map Editor <unsaved map>";
	
	public static float CAMERA_OFFSETX = 0;
	public static float CAMERA_OFFSETY = 0;

	public MapEditor() throws SlickException {
		super(title);
		
		frame = new JFrame(title);
		
		// override default close (because broken)
		
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				System.out.println("exit");
				System.exit(0);
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				System.exit(0);
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		frame.setLocationRelativeTo(null);
		frame.setSize(800,800);
		
		CanvasGameContainer app = new CanvasGameContainer(this);
		app.setBounds(20, 20, 800, 800);

		frame.add(app, BorderLayout.CENTER);
		
		JMenuBar menuBar = new JMenuBar();
		JMenu file = new JMenu("File");
		menuBar.add(file);
		
		newItem = new JMenuItem("New");
		openItem = new JMenuItem("Open");
		saveItem = new JMenuItem("Save");
		
		newItem.addActionListener(this);
		openItem.addActionListener(this);
		saveItem.addActionListener(this);
		
		file.add(newItem);
		file.add(openItem);
		file.add(saveItem);
		
		frame.setJMenuBar(menuBar);
		
		frame.setVisible(true);
		
		app.start();
	}

	@Override
	public void render(GameContainer gc, Graphics g) throws SlickException {
		if(level != null){
			level.draw(g, null);
		}
	}

	@Override
	public void init(GameContainer gc) throws SlickException {
		ascii = new AsciiLevel();
	}

	@Override
	public void update(GameContainer gc, int delta) throws SlickException {
		Input input = gc.getInput();

		//camera input
		if(input.isKeyDown(Input.KEY_A)) {
			CAMERA_OFFSETX+=0.5f;
		} else if (input.isKeyDown(Input.KEY_D)) {
			CAMERA_OFFSETX-=0.5f;
		} else if(input.isKeyDown(Input.KEY_W)) {
			CAMERA_OFFSETY+=0.5f;
		} else if(input.isKeyDown(Input.KEY_S)) {
			CAMERA_OFFSETY-=0.5f;
		} else if(input.isKeyDown(Input.KEY_R)) {
			CAMERA_OFFSETX = 0;
			CAMERA_OFFSETY = 0;
		}

		//mouse input
		if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
			int x = input.getMouseX();
			int y = input.getMouseY();
			
			
		} else if(input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON)){
			int x = input.getMouseX();
			int y = input.getMouseY();
		
			ascii.lights.add((x+CAMERA_OFFSETX) + "\t" + (y+CAMERA_OFFSETY));
		}
	}

	public static void main(String[] args) throws SlickException {
		MapEditor me = new MapEditor();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		
		if("New".equals(cmd)){
			String w = JOptionPane.showInputDialog("Width?");
			String h = JOptionPane.showInputDialog("Height?");
			
			try{
				int width = Integer.parseInt(w);
				int height = Integer.parseInt(h);
				
				ascii = new AsciiLevel();
				
				ascii.width = width;
				ascii.height = height;
				
			} catch(NumberFormatException ex){
				JOptionPane.showMessageDialog(frame, "Width or height not integers!");
			}
			
		} else if("Save".equals(cmd)){
			
		} else if("Load".equals(cmd)){
			
		} else if("update".equalsIgnoreCase(cmd)){
			try {
				level = new Level(ascii);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (SlickException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
	
}
